<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
        #title > h1 {
            color: #aa0719;
            margin-left: 300px;
        }
    </style>
</head>
<body>
<div id="title">
    <h1>查看商家</h1>
</div>
<hr>
<div>
    <input style="margin-left: 500px;margin-top:30px;float: left;  width: 500px;height: 50px" type="text"
           class="form-control" id="sname" placeholder="请输入商家名称"/>
    &emsp;&emsp;&emsp;&emsp;&emsp;<button style="width: 80px;height: 50px ;margin-top:30px;" type="button"
                                          class="btn btn-info" id="queryBtn">查询
</button>
</div>
<hr>
<div class="container">
    <table class="table table-striped">
        <thead>
        <th>商家编号</th>
        <th>商家名称</th>
        <th>商家类别</th>
        <th>商家地址</th>
        <th>联系方式</th>
        <th>注册时期</th>
        </thead>
        <tbody></tbody>
    </table>
    <div class="text-right">
        <span id="showPage">共0条数据，当前0/0页</span>
        <div class="btn-group" style="display: inline-block">
            <button type="button" class="btn btn-group-sm" id="btnFirst">首页</button>
            <button type="button" class="btn btn-group-sm" id="btnPrev">上一页</button>
            <button type="button" class="btn btn-group-sm" id="btnNext">下一页</button>
            <button type="button" class="btn btn-group-sm" id="btnLast">末页</button>
        </div>
    </div>
</div>
</body>
<script src="js/jquery-3.2.1.js"></script>
<script src="js/layer/layer.js"></script>
<script>
    var currentPage = 1;
    var totalPage = 0;
    $(function () {
        $('tbody').empty();
        $.post('store.queryAll', function (data) {
            if (data.code == 200) {
                $('#showPage').text('共' + data.page.totalRecord + '条数据，当前' + currentPage + '/' + data.page.totalPage + '页')
                totalPage = data.page.totalPage;
                $.each(data.result, function (index, obj) {
                    var tr = $('<tr></tr>');
                    var td1 = $('<td></td>').text(obj.sid);
                    var td2 = $('<td></td>').text(obj.sname);
                    var td3 = $('<td></td>').text(obj.stype);
                    var td4 = $('<td></td>').text(obj.saddress);
                    var td5 = $('<td></td>').text(obj.phone);
                    var td6 = $('<td></td>').text(obj.sdate);
                    var td7 = $('<td></td>').html('<button type="button" onclick="editStore(' + obj.sid + ')" style="height: 40px" class="btn btn-primary ">编辑</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" onclick="delStore(' + obj.sid + ')" style="height: 40px" class="btn btn-danger btn1">删除</button>');
                    tr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6).append(td7);
                    $('tbody').append(tr);
                })
            } else {
                layer.msg(data.msg);
            }
        }, 'json')

        $('#queryBtn').click(function () {
            QueryByname($('#sname').val());
        })
        $('#btnTypeSearch').click(function () {
            QueryByname($('#sname').val());
        })


        $('#btnNext').click(function () {
            currentPage++;
            if (currentPage > totalPage) {
                currentPage = totalPage;
            }
            QueryByname($('#sname').val());
        })
        $('#btnPrev').click(function () {
            currentPage--;
            if (currentPage < 0) {
                currentPage = 1;
            }
            QueryByname($('#sname').val());
        })
        $('#btnFirst').click(function () {
            currentPage = 1;
            QueryByname($('#sname').val());
        })
        $('#btnLast').click(function () {
            currentPage = totalPage;
            QueryByname($('#sname').val());
        })


    })
    function QueryByname(sname) {
        $('tbody').empty();
        $.get('store.queryBySname', {
            sname: sname.trim(),
            currPage: currentPage
        }, function (data) {
            if (data.code == 200) {
                $('#showPage').text('共' + data.page.totalRecord + '条数据，当前' + currentPage + '/' + data.page.totalPage + '页')
                totalPage = data.page.totalPage;
                $.each(data.result, function (index, obj) {
                    var tr = $('<tr></tr>');
                    var td1 = $('<td></td>').text(obj.sname);
                    var td2 = $('<td></td>').text(obj.stype);
                    var td3 = $('<td></td>').text(obj.saddress);
                    var td4 = $('<td></td>').text(obj.phone);
                    var td5 = $('<td></td>').text(obj.sdate);
                    var td6 = $('<td></td>').html('<button type="button" onclick="editStore(' + obj.sid + ')" style="height: 40px" class="btn btn-primary ">编辑</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" onclick="delStore(' + obj.sid + ')" style="height: 40px" class="btn btn-danger btn1">删除</button>');
                    tr.append(td1).append(td2).append(td3).append(td4).append(td5).append(td6);
                    $('tbody').append(tr);
                })
            } else {
                layer.msg(data.msg);
            }
        }, 'json')
    }
    function editStore(sid) {
        location.href = "editStore.html?sid=" + sid;
    }

    function delStore(sid) {
        layer.confirm('是否确认删除', {icon: 3, title: '删除提示'}, function (index) {
            $.getJSON('store.delete', {sid: sid}, function (data) {
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                        location.href = "Managermain.html";
                    })
                }
            })
            layer.close(index);
        });

    }
</script>
</html>